package newfish.contacts.core.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import newfish.contacts.core.entity.impl.Contact;
import newfish.contacts.core.entity.impl.User;
import newfish.contacts.core.service.UserService;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements RequestAware,
		SessionAware {
	
	private Map request;
	private Map session;
	
	private UserService userService;
	private List<User> userList;
	private User user;
	private String jsonString;
	private boolean init;
	
	public String create(){
		userService.create(user);
		toJsonString(user);
		return SUCCESS;
	}
	
	public String delete(){
		userService.delete(user);
		jsonString = SUCCESS;
		return SUCCESS;
	}
	
	public String update(){
		userService.update(user);
		jsonString = SUCCESS;
		return SUCCESS;
	}
	
	public String findById(){
		user = userService.findById(user.getId(),init);
		jsonString = JSONArray.fromObject(user.getContacts()).toString();
		System.out.println(jsonString);
		return SUCCESS;
	}
	
	public String listAll(){
		userList = userService.listAll();
		JsonConfig jConfig = new JsonConfig();
		jConfig.setExcludes(new String[]{"contacts"});
		JSONArray json = JSONArray.fromObject(userList,jConfig);
		jsonString = json.toString();
		System.out.println(jsonString);
		return SUCCESS;
	}
	
	private void toJsonString(User user){
		JSONObject json = JSONObject.fromObject(user);
		jsonString = json.toString();
	}
/* ********************************************************************** */
	@Override
	public void setRequest(Map request) {
		this.request = request;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public boolean isInit() {
		return init;
	}

	public void setInit(boolean init) {
		this.init = init;
	}
	
	public static void main(String[] args) {
//		User u = new User();
//		u.setId("abc");
//		u.setName("newname");
//		Contact c = new Contact();
//		c.setMsn("a@live.cn");
//		Contact c1 = new Contact();
//		c1.setMsn("a@live.cn");
//		u.getContacts().add(c);
//		u.getContacts().add(c1);
//		
//		String[] strs = new String[]{"a","d","c"};
//	
//		System.out.println(JSONArray.fromObject(strs).toString());
	}
	
}
